/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.google.code.jmarias.client.tcp;

import com.google.code.jmarias.common.action.AbstractAction;
import com.google.code.jmarias.common.action.PickCardFromPeopleAction;
import com.google.code.jmarias.common.action.PickedTrumpAction;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author Ondrej Michal
 */
public class NewClass3 {

    private Set<ActionListener> listeners = new HashSet<ActionListener>();
    private String state;

//    public static Set<Class> getSupportedActions() {
//        Set<Class> result = new HashSet<Class>();
//        Set<ActionListener> set = new NewClass3().listeners;
//        for (ActionListener listener : set) {
//            result.add(listener.acceptedAction);
//        }
//        return result;
//    }

    public NewClass3() {
        registerListeners();
    }

    private void addListener(ActionListener listener) {
        listeners.add(listener);
    }

    private void registerListeners() {
        addListener(new ActionListener(PickCardFromPeopleAction.class) {

            @Override
            protected void processAction(AbstractAction action, String state) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

        });

        addListener(new ActionListener(PickedTrumpAction.class) {

            @Override
            protected void processAction(AbstractAction action, String state) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

        });
    }


    /** Musi vyhovovat jen jeden listener, jinak konci vyjimkou. */
    public void processAction(AbstractAction action) {
        int i = 0;
        for (ActionListener listener : listeners) {
            if (listener.accepts(action)) {
                i++;
                if (i > 1) {
                    throw new RuntimeException("Chyba serveru - akci " + action + " zpracovava vice listeneru.");
                }
                listener.actionPerformed(action, state);
            }
        }
        if (i == 0) {
            System.out.println("Akce nebyla zachycena zadnym listenerem, zrejme tedy slo o nepovolenou akci, a proto byla ignorovana.");
        }
    }

    private abstract static class ActionListener {

        public void actionPerformed(AbstractAction action, String state) {
            processAction(action, state);
        }

        protected abstract void processAction(AbstractAction action, String state);

        private Class acceptedAction;

        public ActionListener(Class acceptedAction) {
            this.acceptedAction = acceptedAction;
        }

        public boolean accepts(AbstractAction action) {
            return acceptedAction.isAssignableFrom(action.getClass());
        }

        @Override
        public String toString() {
            return "listener " + acceptedAction;
        }

    }

    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
//        new NewClass().processAction(null);
//        new NewClass().processAction(null);

//        Class clazz = NewClass3.class;
//
//        System.out.println("asdfůlasdf: " + ((NewClass3) clazz.newInstance()).listeners);

        NewClass3 clazz = new NewClass3();

        System.out.println(clazz.listeners);


        clazz.state = "stav1";
        clazz.processAction(new PickCardFromPeopleAction());
    }
}
